package yibao.youyun.com.yibao.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import yibao.youyun.com.yibao.R;

public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.ViewHolder>
{

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private LayoutInflater mInflater;
    private int[] mDatas;
    Context context;

    public VerticalAdapter(Context context, int[] datats)
    {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        ImageView mImg;
        TextView mTxt1;
        TextView mTxt2;
        TextView mTxt3;
        TextView mTxt4;
    }

    @Override
    public int getItemCount()
    {
        return mDatas.length;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.home_layout_list_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (ImageView) view
                .findViewById(R.id.list_item_imageview);
        viewHolder.mTxt1 = (TextView) view
                .findViewById(R.id.list_item_textview1);
        viewHolder.mTxt2 = (TextView) view
                .findViewById(R.id.list_item_textview2);
        viewHolder.mTxt3 = (TextView) view
                .findViewById(R.id.list_item_textview3);
        viewHolder.mTxt4 = (TextView) view
                .findViewById(R.id.list_item_textview4);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        Picasso.with(context)
                .load(mDatas[i])
                .placeholder(R.mipmap.ic_launcher)
                .resize(180, 100)
                .into(viewHolder.mImg);
        if (mOnItemClickLitener != null)
        {
            viewHolder.itemView.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });

        }

    }

}
